package com.zzy.util.file;

import javax.swing.filechooser.FileSystemView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 操作 txt 的 工具类
 */
public class UtilTxtFile {

    public static boolean createFile(File fileName) {
        boolean flag = false;
        try {
            if (!fileName.exists()) {
                fileName.createNewFile();
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 读TXT文件内容
     * @param file  [地址加上文件名字]
     * @return
     */
    public static String readTxtFile(File file) {
        String result = "";
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String read = null;
            while ((read = bufferedReader.readLine()) != null) {
                result = result + read + "\r\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 生成一个file txt [如果有该文件则会替换]
     * @param content
     * @param file 【生成的文件的地址带文件名字】
     * @throws Exception
     */
    public static boolean writeTxtFile(String content, File file) {
        RandomAccessFile mm = null;
        boolean flag = false;
        FileOutputStream o = null;
        try {
            o = new FileOutputStream(file);
            o.write(content.getBytes());
            //o.write(content.getBytes("GBK"));
            o.close();
            // mm=new RandomAccessFile(fileName,"rw");
            // mm.writeBytes(content);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mm != null) {
                try {
                    mm.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }

    /**
     * 向文件中追加 内容
     * @param filePath [文件的地址带文件名字]
     * @param content 文件要追加的内容
     */
    public static void appendContentToTxt(String filePath, String content) {
        String str = new String(); // 原有txt内容
        String s1 = new String();// 内容更新
        try {
            File f = new File(filePath);
            if (!f.exists()){
                f.createNewFile();// 不存在则创建
            }
            BufferedReader input = new BufferedReader(new FileReader(f));
            while ((str = input.readLine()) != null) {
                s1 += str + "\n";
            }
            input.close();
            s1 += content;
            BufferedWriter output = new BufferedWriter(new FileWriter(f));
            output.write(s1);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //1、创建 并且写 txt
        String projectDir = System.getProperty( "user.dir" );
        String dir = projectDir + "\\provider-file\\src\\main\\resources\\txt\\";
        String fileName = dir + "for_create.txt";
        //a、覆盖更新
        String content = System.currentTimeMillis() + "-这是内容"  +"\n";
        writeTxtFile(content,new File(fileName));
        // b、追加
        String appendContent = System.currentTimeMillis() + "-这是追加的内容"+ "\n";
        appendContentToTxt(fileName,appendContent);
        //2、读 txt
        String s = readTxtFile(new File(fileName));
        System.out.println(s);


    }

}
